<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
    <script src="../../js/lay-module/http/http.js"></script>
</head>
<body onload="init()">
<div class="layui-form layuimini-form">
    <div class="layui-form-item" hidden>
        <label class="layui-form-label required"></label>
        <div class="layui-input-block">
            <input type="text" name="aId"  id="aId" placeholder="请输入用户帐号" value="" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" id="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题" value="" class="layui-input">
            <tip>填写公告的标题。</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">内容</label>
        <div class="layui-input-block">
            <textarea name="content" id="content" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-inline" lay-filter="classDiv">
            <select id="type" name="type" lay-filter="classIdFilter">
                <option value="0">系统公告</option>
                <option value="1">通知公告</option>
            </select>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    function init(){
        var $ = layui.jquery

        // 2.获取用户当前的信息
        $.ajax({
            type: 'GET',
            url: $api + 'assit/notice/select/' + unescape(getQueryString("aId")),
            contentType: "application/json;charset=utf-8",
            async: false,
            headers: {
                'Authorization': layui.data('table')['Authorization']
            },
            success: function (res) {
                // 2.1 根据查询到的数据对页面进行填充
                identifyCode(res)
                if (!res.code) {
                    // 设置<select>当前的选中项
                    var selectType = 'dd[lay-value=' + res.data.type + ']';
                    $("#aId").val(res.data.aid);

                    $("#title").val(res.data.title);
                    $("#content").val(res.data.content);
                    $('#type').siblings("div.layui-form-select").find('dl').find(selectType).click();
                }else{
                    layer.msg(res.message)
                }

            }
        });
    }
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            // console.log(JSON.stringify(data.field))
            var index = layer.confirm("是否提交", {
                title: '最终的提交信息'
            }, function () {
                $.ajax({
                    type: 'POST',
                    url: $api + 'assit/notice/save',
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(data.field),
                    dataType: 'json',
                    async: false,
                    headers: {
                        'Authorization': layui.data('table')['Authorization']
                    },
                    success: function (res) {
                        // 2.1 根据查询到的数据对页面进行填充
                        identifyCode(res)
                        if (!res.code) {
                            // 关闭弹出层
                            layer.close(index);

                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            // 重新加载父类
                            parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新

                            parent.layer.close(iframeIndex);
                        }else{
                            layer.msg(res.message)
                        }

                    }
                });


            });

            return false;
        });

    });
</script>
</body>
</html>